package org.tulkas.robot.web;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.tulkas.robot.base.GlobalConfig;
import org.tulkas.robot.base.PersistenceHelper;
import org.tulkas.robot.log.LogManager;

/**
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public class ContextInitListener implements ServletContextListener {

	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent sce) {
		Logger logger = Logger.getLogger(ContextInitListener.class.getName());
		logger.info("Tulkas Robot destroying.");
	}

	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent sce) {
		String homePath = sce.getServletContext().getInitParameter(
				"tulkas.robot.home");
		if (homePath != null) {
			GlobalConfig.init(homePath);
		}

		Logger logger = Logger.getLogger(ContextInitListener.class.getName());
		logger.info("Tulkas Robot initialization begin.");
		try {
			PersistenceHelper.initDerbyDriver();
			LogManager.getInstance().init();
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Initialization Error. []"
					+ e.getMessage(), e);
		}
	}

}
